package com.campus.util;

import org.springframework.beans.BeanUtils;

import java.lang.reflect.Constructor;

/**
 * @Author: AlaskaBoo
 * @Date: 2022-11-16 19:22
 **/
public class CopyPropertiesUtils {

    private CopyPropertiesUtils() {}

    public static <T> T copyProperties(Object source, Class<T> targetClass) {
        T target;
        try {
            Constructor<T> constructor = targetClass.getDeclaredConstructor();
            target = constructor.newInstance();
            BeanUtils.copyProperties(source, target);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return target;
    }
}
